package com.tssm.core.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public  class AbstractFieldValueUtil<T> {
	/**
	 * 
	 * @方法说明：获得Bean的名称和值
	 * @author 徐龙
	 * @date Sep 13, 2010
	 * @param t
	 *            Bean对象
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getParamVale(T t) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Field[] fiel = t.getClass().getDeclaredFields();
		for (int i = 0; i < fiel.length; i++) {
			String paramName = fiel[i].getName();
			String getNameValue = "get"
					+ paramName.substring(0, 1).toUpperCase()
					+ paramName.substring(1);
			Method mt = t.getClass().getDeclaredMethod(getNameValue, null);// 得到get方法
			Object ovalue = (Object) mt.invoke(t, null);// 得到get方法的值
			map.put(paramName, ovalue);
		}
		return map;
	}

	/**
	 * 
	 * 
	 * @方法说明:得到对象指定的参数的值
	 * @param t
	 * @param param
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @作者:徐龙
	 * @日期:2011-5-17
	 */
	public Object getValueByParam(T t, String param)
			throws IllegalArgumentException, IllegalAccessException,
			InvocationTargetException, SecurityException, NoSuchMethodException {
		String getNameValue = "get" + param.substring(0, 1).toUpperCase()
				+ param.substring(1);
		Method mt = t.getClass().getDeclaredMethod(getNameValue, null);// 得到get方法
		return (Object) mt.invoke(t, null);// 得到get方法的值
	}

	/**
	 * 
	 * 
	 * @方法说明:得到对象指定的参数的值
	 * @param t
	 * @param param
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @作者:徐龙
	 * @日期:2011-5-17
	 */
	public String getStringValueByParam(T t, String param)
			throws IllegalArgumentException, IllegalAccessException,
			InvocationTargetException, SecurityException, NoSuchMethodException {
		Object fieldObject = this.getValueByParam(t, param);
		String fieldValue = null;
		if (fieldObject instanceof Integer) {
			fieldValue = fieldObject.toString();
		} else if (fieldObject instanceof String) {
			fieldValue = (String) fieldObject;
		} else {
			fieldValue = (String) String.valueOf(fieldObject);
		}
		return fieldValue;
	}

	/**
	 * 
	 * 
	 * @方法说明: 设置对象指定的参数的值
	 * @param t 对象
	 * @param paramValue 参数的值
	 * @param fs 指定的参数的Filed对象
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @作者:徐龙
	 * @日期:2011-5-17
	 */
	public final void setValueByParam(T t, Object paramValue, Field fs)
			throws SecurityException, NoSuchMethodException,
			IllegalArgumentException, IllegalAccessException,
			InvocationTargetException {
		String param = fs.getName();
		String setNameValue = "set" + param.substring(0, 1).toUpperCase()
				+ param.substring(1);
		Method mt = t.getClass().getDeclaredMethod(setNameValue, fs.getType());// 得到set方法
		mt.invoke(t, paramValue);// 设置set方法的值
	}
}
